// ClassRemapper.groovy
package com.kasax.plugin.shadow

import org.objectweb.asm.ClassVisitor
import com.kasax.plugin.shadow.utils.LogUtils
class ClassRemapper extends ClassVisitor {
    private final PackageRemapper remapper

    ClassRemapper(ClassVisitor cv, PackageRemapper remapper) {
        super(Opcodes.ASM9, cv)
        this.remapper = remapper
        LogUtils.d("ClassRemapper", "ClassRemapper initialized with remapper: ${remapper.getClass().simpleName}")
    }

    @Override
    void visit(int version, int access, String name, String signature, String superName, String[] interfaces) {
        def n = remapper.map(name)
        def s = remapper.map(superName)
        def i = interfaces?.collect { remapper.map(it) } as String[]
        super.visit(version, access, n, signature != null ? remapper.mapDesc(signature) : null, s, i)
    }

    @Override
    void visitInnerClass(String name, String outerName, String innerName, int access) {
        def nn = remapper.map(name)
        def no = outerName != null ? remapper.map(outerName) : null
        super.visitInnerClass(nn, no, innerName, access)
    }

    @Override
    void visitOuterClass(String owner, String name, String descriptor) {
        def no = remapper.map(owner)
        def nd = name != null ? remapper.mapDesc(descriptor) : null
        super.visitOuterClass(no, name, nd)
    }

}